﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Windows.Forms;
using common;
using MySql.Data.MySqlClient;

namespace WindowsApplication2._7_访问数据.数据库.test
{
    public partial class 综合 : Form
    {
        public string str_type = "";
        public string str_init = "";

        /// <summary>
        ///
        /// </summary>
        public 综合()
        {
            InitializeComponent();
        }

        /// <summary>
        ///
        /// </summary>
        /// <param name="args"></param>
        public 综合(string args)
        {
            InitializeComponent();
        }

        /// <summary>
        ///
        /// </summary>
        /// <param name="args"></param>
        public 综合(string[] args)
        {
            InitializeComponent();

            string s1 = "";
            foreach (string s in args)
            {
                s1 = s1 + s + " ";
            }

            string sql = s1;
            connect_database();
            this.tabControlMain.SelectedTab = this.tabPage2;
            this.richTextBox1.Text = sql;

            ExcuteSql(sql);
        }

        /// <summary>
        /// 连接数据库
        /// </summary>
        private void connect_database()
        {
            try
            {
                //*********************************************************
                //
                //*********************************************************
                //if (this.textBoxServer.Text == "")
                //{
                //    MessageBox.Show("非空"); return;
                //}

                //清除treeview
                treeView1.Nodes.Clear();
                TreeNode tn1 = new TreeNode();
                tn1.Text = this.textBoxServer.Text.ToString();
                tn1.Tag = "root";
                treeView1.Nodes.Add(tn1);

                //*********************************************************
                //
                //*********************************************************
                string datasource = this.textBoxServer.Text;
                //string database = this.textBoxDataBase.Text;
                string uid = this.textBoxUid.Text;
                string pwd = this.textBoxPass.Text;
                string type = this.textBox_type.Text;
                string database = this.textBox_database.Text;

                //*********************************************************
                //
                //*********************************************************
                if (type == "mysql")
                {
                    string strCon = "server=" + datasource + ";user id=" + uid + "; pooling=false;charset=gb2312; password=" + pwd + "; Allow Zero Datetime=True";
                    MysqlHelper.strConnString = strCon;
                    string strSql = "SHOW DATABASES";

                    MySql.Data.MySqlClient.MySqlDataReader thisReader = MysqlHelper.GetReader(strSql);

                    DataTable dt1 = new DataTable();
                    dt1.Columns.Add("数据库");

                    while (thisReader.Read())
                    {
                        DataRow dr1 = dt1.NewRow();
                        dr1[0] = thisReader[0].ToString();
                        dt1.Rows.Add(dr1);

                        TreeNode tn = new TreeNode();
                        tn.Text = thisReader[0].ToString();
                        tn.Tag = "database";
                        this.treeView1.Nodes[0].Nodes.Add(tn);
                    }

                    this.dataGridViewTable.DataSource = dt1;
                    thisReader.Close();
                }

                else if (type == "sqlserver")
                {
                    string strCon = "";
                    string strSql = "";

                    if (this.textBox_database.Text != "")
                    {
                        strCon = "server=" + datasource + ";database=" + database + ";uid=" + uid + ";pwd=" + pwd + ";";
                        SqlServerHelper.strConnString = strCon;

                        treeView1.Nodes.Clear();
                        TreeNode tn = new TreeNode();
                        tn.Text = textBox_database.Text;
                        tn.Tag = "database";
                        tn.Checked = true;
                        treeView1.Nodes.Add(tn);

                        strSql = "use " + textBox_database.Text + ";" + "select * from sysobjects where type='U' order by name";
                        DataTable dt1 = SqlServerHelper.GetDataTable(strSql);
                        this.dataGridViewTable.DataSource = dt1;
                    }
                    else
                    {
                        //strCon = "server=" + datasource + ";database=" + "" + ";uid=" + uid + ";pwd=" + pwd + ";";
                        strCon = @"User ID=sa;Password=111111;Initial Catalog=KBS_REMOTE;Data Source=DBJB0019\ZR;Max Pool Size=1000;DATABASE=KBS_REMOTE";

                        SqlServerHelper.strConnString = strCon;
                        strSql = "select * from sys.sysdatabases ";

                        System.Data.SqlClient.SqlDataReader thisReader = SqlServerHelper.GetReader(strSql);

                        DataTable dt1 = new DataTable();
                        dt1.Columns.Add("数据库");

                        while (thisReader.Read())
                        {
                            DataRow dr1 = dt1.NewRow();
                            dr1[0] = thisReader[0].ToString();
                            dt1.Rows.Add(dr1);

                            TreeNode tn = new TreeNode();
                            tn.Text = thisReader[0].ToString();
                            tn.Tag = "database";
                            this.treeView1.Nodes[0].Nodes.Add(tn);
                        }

                        this.treeView1.Sort();

                        this.dataGridViewTable.DataSource = dt1;
                        thisReader.Close();
                    }
                }
                else if (type == "sqllite")
                {
                    if (this.textBox_file.Text.Trim() == "") { return; }
                    string file = this.textBox_file.Text.Trim();
                    common.SqliteHelper.strConnString = "Data Source=" + file;
                    //SqliteHelper.ExecuteNonQuery(".tables");
                    //DataTable dt = SqliteHelper.GetDataTable(".tables");
                }
            }

            catch (Exception ee)
            {
                MessageBox.Show("连接失败....");
            }
        }

        /// <summary>
        /// 连接
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonTestConect_Click(object sender, EventArgs e)
            
        {
            try
            {
                this.connect_database();
            }
            catch (Exception ee)
            {

                MessageBox.Show(ee.ToString());
            }
           
        }

        private void BindTree()
        {
        }

        /// <summary>
        /// 加载
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void 综合_Load(object sender, EventArgs e)
        {
            this.textBox_type.Items.Add("sqlserver");
            this.textBox_type.Items.Add("mysql");
            this.textBox_type.Items.Add("sqllite");
            this.textBox_type.Items.Add("access");
            this.textBox_type.Text = "sqlserver";

            //textBox_type.Text = "";
            textBoxServer.Text = "ZR-PC\\MYMSSQLSERVER";
            textBoxUid.Text = "sa";
            textBoxPass.Text = "111111";
            textBox_database.Text = "master";

            //if (str_init != "")
            //{
            //    string[] s = str_init.Split('\n');
            //    this.textBox_type.Text = s[0].Replace("\r", "");
            //    this.textBoxServer.Text = s[1].Replace("\r", "");
            //    this.textBoxUid.Text = s[2].Replace("\r", "");
            //    this.textBoxPass.Text = s[3].Replace("\r", "");

            //    if (s.Length > 4)
            //    {
            //        this.textBox_database.Text = s[4].Replace("\r", "");
            //    }

            //}

            //string strCon = "server=" + "localhost" + ";user id=" + "root" + "; pooling=false;charset=gb2312; password=" + "" + "; Allow Zero Datetime=True";
            //MysqlHelper.strConnString = strCon;

            //DataTable dt = MysqlHelper.GetDataTable("select * from small_project.database");
            //foreach (DataRow dr in dt.Rows)
            //{
            //    string result = "";
            //    result += "name:" + dr["name"] + ";";
            //    result += "server:" + dr["server"] + ";";
            //    result += "user:" + dr["user"] + ";";
            //    result += "password:" + dr["password"] + ";";
            //    result += "type:" + dr["type"] + ";";

            //    this.listBox1.Items.Add(result);
            //}

            //string result = "";
            //result += "name:" +"neimeng" + ";";
            //result += "server:" + "10.192.34.195" + ";";
            //result += "user:" + "sa" + ";";
            //result += "password:" + "Pass@word1" + ";";
            //result += "type:" + "sqlserver" + ";";
            //this.listBox1.Items.Add(result);
        }

        /// <summary>
        /// listbox
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        //private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        //{
        //    try
        //    {
        //        string result = listBox1.SelectedItem.ToString();
        //        string[] result1 = result.Split(';');

        //        this.textBoxServer.Text = result1[1].Replace("server:", "").Trim();
        //        this.textBoxUid.Text = result1[2].Replace("user:", "").Trim();
        //        this.textBoxPass.Text = result1[3].Replace("password:", "").Trim();
        //        this.textBox_type.Text = result1[4].Replace("type:", "").Trim();
        //        str_type = this.textBox_type.Text;
        //    }
        //    catch (Exception ee)
        //    {
        //    }
        //}

        /// <summary>
        /// 点击树
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            try
            {
                if (this.treeView1.SelectedNode.Tag.ToString() == "root")
                {
                    treeView1.SelectedNode.Expand();
                    return;
                }

                treeView1.SelectedNode.Nodes.Clear();

                //*********************************************************
                //数据库
                //*********************************************************
                if (this.treeView1.SelectedNode.Tag.ToString() == "database")
                {
                    if (str_type == "mysql")
                    {
                        string strSql = "show tables from " + treeView1.SelectedNode.Text;

                        MySqlDataReader thisReader = MysqlHelper.GetReader(strSql);
                        while (thisReader.Read())
                        {
                            TreeNode tn = new TreeNode();
                            tn.Text = thisReader[0].ToString();
                            tn.Tag = "table";

                            treeView1.SelectedNode.Nodes.Add(tn);
                            treeView1.SelectedNode.Expand();
                        }

                        thisReader.Close();
                    }
                    else if (str_type == "sqlserver")
                    {
                        string strSql = "use " + treeView1.SelectedNode.Text + ";" + "select * from sysobjects where type='U' order by name";

                        System.Data.SqlClient.SqlDataReader thisReader = SqlServerHelper.GetReader(strSql);
                        while (thisReader.Read())
                        {
                            TreeNode tn = new TreeNode();
                            tn.Text = thisReader[0].ToString();
                            tn.Tag = "table";

                            treeView1.SelectedNode.Nodes.Add(tn);
                            treeView1.SelectedNode.Expand();
                        }

                        thisReader.Close();
                    }
                }

                //*********************************************************
                //表
                //*********************************************************
                else if (this.treeView1.SelectedNode.Tag.ToString() == "table")
                {
                    if (str_type == "mysql")
                    {
                        //*********************************************************
                        //
                        //*********************************************************
                        string strDataBase = this.treeView1.SelectedNode.Parent.Text;
                        string strTable = this.treeView1.SelectedNode.Text;
                        string strSql = "select * from " + strDataBase + "." + strTable + " limit 0,100";

                        this.dataGridView1.DataSource = MysqlHelper.GetDataTable(strSql);

                        //*********************************************************
                        //SHOW full COLUMNS FROM assets.device
                        //*********************************************************
                        //strSql = "SHOW COLUMNS FROM " + strDataBase + "." + strTable + "";
                        strSql = "SHOW full COLUMNS FROM " + strDataBase + "." + strTable + "";
                        DataTable dt = MysqlHelper.GetDataTable(strSql);
                        foreach (DataRow dr in dt.Rows)
                        {
                            string field = dr["field"].ToString();
                            string type = dr["type"].ToString();
                            string nullvalue = dr["null"].ToString();
                            string key = dr["key"].ToString();
                            string extra = dr["extra"].ToString();
                            string comment = dr["comment"].ToString();

                            string result = "";
                            result += field;
                            result += " [";
                            result += type + ",";

                            if (nullvalue.ToLower() == "yes")
                            {
                                result += "null" + ",";
                            }
                            else
                            {
                                result += "not null" + ",";
                            }

                            result += key + ",";

                            result += extra + ",";

                            result += comment + "]";

                            TreeNode tn = new TreeNode();
                            tn.Text = result;
                            tn.Tag = "column";
                            treeView1.SelectedNode.Nodes.Add(tn);
                        }
                    }

                    else if (str_type == "sqlserver")
                    {
                        //*********************************************************
                        //
                        //*********************************************************
                        string strDataBase = this.treeView1.SelectedNode.Parent.Text;
                        string strTable = this.treeView1.SelectedNode.Text;
                        string strSql = "select top 100 * from " + strDataBase + ".." + strTable + "";

                        this.dataGridView1.DataSource = SqlServerHelper.GetDataTable(strSql);

                        //*********************************************************
                        //SHOW full COLUMNS FROM assets.device
                        //*********************************************************
                        //strSql = "SHOW COLUMNS FROM " + strDataBase + "." + strTable + "";
                        //strSql = "select   *   from   syscolumns where id=object_id('" + strDataBase + ".." + strTable + "')" ;
                        strSql = "use " + strDataBase + ";select   a.[name] as lieming,b.[name] as leixing    from   syscolumns a,systypes b  where   a.id=object_id('" + strDataBase + ".." + strTable + "') and a.xtype=b.xtype and    b.[name]<>'sysname'";

                        DataTable dt = SqlServerHelper.GetDataTable(strSql);
                        foreach (DataRow dr in dt.Rows)
                        {
                            string field = dr["lieming"].ToString();
                            string type = dr["leixing"].ToString();
                            //string nullvalue = dr["null"].ToString();
                            //string key = dr["key"].ToString();
                            //string extra = dr["extra"].ToString();
                            //string comment = dr["comment"].ToString();

                            string result = "";
                            result += field;
                            result += " [";
                            result += type + "]";

                            //if (nullvalue.ToLower() == "yes")
                            //{
                            //    result += "null" + ",";
                            //}
                            //else
                            //{
                            //    result += "not null" + ",";
                            //}

                            //result += key + ",";

                            //result += extra + ",";

                            //result += comment + "]";

                            TreeNode tn = new TreeNode();
                            tn.Text = result;
                            tn.Tag = "column";
                            treeView1.SelectedNode.Nodes.Add(tn);
                        }
                    }
                }
            }
            catch (Exception ee)
            {
            }
        }

        private void ExcuteSql(string strSql)
        {
            try
            {
                if (str_type == "mysql")
                {
                    MySqlDataReader thisReader = MysqlHelper.GetReader(strSql);
                    DataTable dt = new DataTable();
                    for (int i = 0; i < thisReader.FieldCount; i++)
                    {
                        dt.Columns.Add(thisReader.GetName(i));
                    }
                    while (thisReader.Read())
                    {
                        DataRow dr1 = dt.NewRow();
                        for (int i = 0; i < thisReader.FieldCount; i++)
                        {
                            dr1[i] = thisReader[i].ToString();
                        }
                        dt.Rows.Add(dr1);
                    }
                    this.dataGridView1.DataSource = dt;
                    dt = MysqlHelper.DealTable_Str(dt);
                    thisReader.Close();

                    this.tabControl1.SelectedTab = tabPageResult;
                    richTextBoxMessage.Text = "";
                }
                else if (str_type == "sqlserver")
                {
                    DataTable dt = SqlServerHelper.GetDataTable(strSql);

                    //SqlDataReader thisReader = SqlServerHelper.GetReader(strSql);
                    //DataTable dt = new DataTable();
                    //for (int i = 0; i < thisReader.FieldCount; i++)
                    //{
                    //    dt.Columns.Add(thisReader.GetName(i));
                    //}
                    //while (thisReader.Read())
                    //{
                    //    DataRow dr1 = dt.NewRow();
                    //    for (int i = 0; i < thisReader.FieldCount; i++)
                    //    {
                    //        dr1[i] = thisReader[i].ToString();
                    //    }
                    //    dt.Rows.Add(dr1);
                    //}
                    this.dataGridView1.DataSource = dt;
                    //dt = MysqlHelper.DealTable_Str(dt);
                    //thisReader.Close();

                    this.tabControl1.SelectedTab = tabPageResult;
                    richTextBoxMessage.Text = "";
                }
            }
            catch (Exception ee)
            {
                this.tabControl1.SelectedTab = tabPageMessage;
                richTextBoxMessage.Text = ee.ToString();

                this.dataGridView1.DataSource = null;
            }
        }

        /// <summary>
        ///执行
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripLabel1_Click(object sender, EventArgs e)
        {
            zhixing();
        }

        private void zhixing()
        {
            if (this.richTextBox1.SelectedText == "")
            {
                string strSql = richTextBox1.Text;
                ExcuteSql(strSql);
            }
            else
            {
                string strSql = richTextBox1.SelectedText;
                ExcuteSql(strSql);
            }
        }

        private void richTextBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            //if(e.KeyChar)
        }

        private void richTextBox1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode.ToString() == "F5")
            {
                zhixing();
            }
        }

        /// <summary>
        /// 导出结构
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripLabel2_Click(object sender, EventArgs e)
        {
            if (this.treeView1.SelectedNode.Tag.ToString() == "table")
            {
                StringBuilder strTrans = new StringBuilder();

                string database = treeView1.SelectedNode.Parent.Text;
                string table = treeView1.SelectedNode.Text;

                strTrans.Append("SET NAMES utf8;\n");
                strTrans.Append("\n");

                strTrans.Append("create database if not exists`" + database + "`;\n");
                strTrans.Append("\n");
                strTrans.Append("USE`" + database + "`;\n");
                strTrans.Append("\n");

                strTrans.Append("/*Table structure for table`" + table + "` */\n");
                strTrans.Append("\n");
                strTrans.Append("DROP TABLE IF EXISTS`" + table + "`;\n");

                strTrans.Append("CREATE TABLE`" + table + "` (\n");

                string result = "";
                string strSql = "SHOW full COLUMNS FROM " + database + "." + table + "";
                DataTable dt = MysqlHelper.GetDataTable(strSql);
                foreach (DataRow dr in dt.Rows)
                {
                    string field = dr["field"].ToString();
                    string type = dr["type"].ToString();
                    string nullvalue = dr["null"].ToString();
                    string key = dr["key"].ToString();
                    string extra = dr["extra"].ToString();
                    string comment = dr["comment"].ToString();

                    result += "`" + field + "`";
                    result += " ";
                    result += type + " ";

                    if (nullvalue.ToLower() == "yes")
                    {
                        result += "null" + " ";
                    }
                    else
                    {
                        result += "not null" + " ";
                    }

                    if (key.ToLower() == "pri")
                    {
                        result += " PRIMARY KEY " + " ";
                    }
                    else
                    {
                    }

                    result += "COMMENT '" + extra + "', \n";
                }

                result = result.Substring(0, result.LastIndexOf(","));
                strTrans.Append(result);

                strTrans.Append(" \n");
                strTrans.Append(")  DEFAULT CHARSET=latin1;\n");

                string strDes = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
                strDes = strDes.Replace(@"\\", @"\");
                strDes = Path.Combine(strDes, "1.sql");

                Encoding encode = Encoding.UTF8;
                StreamWriter sw = new StreamWriter(strDes, false, encode);
                sw.Write(strTrans.ToString());
                sw.Flush();
                sw.Close();

                Process.Start(strDes);
            }
        }

        private string daochu_table(string database, string table)
        {
            //*********************************************************
            //mysql
            //*********************************************************

            #region MyRegion

            if (str_type == "mysql")
            {
                StringBuilder strTrans = new StringBuilder();

                //string database = treeView1.SelectedNode.Parent.Text;
                //string table = treeView1.SelectedNode.Text;

                //*********************************************************
                //jianku
                //*********************************************************
                strTrans.Append("SET NAMES utf8;\n");
                strTrans.Append("\n");

                strTrans.Append("create database if not exists`" + database + "`;\n");
                strTrans.Append("\n");
                strTrans.Append("USE`" + database + "`;\n");
                strTrans.Append("\n");

                strTrans.Append("/*Table structure for table`" + table + "` */\n");
                strTrans.Append("\n");
                strTrans.Append("DROP TABLE IF EXISTS`" + table + "`;\n");

                strTrans.Append("CREATE TABLE`" + table + "` (\n");

                string result = "";
                string strSql = "SHOW full COLUMNS FROM " + database + "." + table + "";
                DataTable dt = MysqlHelper.GetDataTable(strSql);
                foreach (DataRow dr in dt.Rows)
                {
                    string field = dr["field"].ToString();
                    string type = dr["type"].ToString();
                    string nullvalue = dr["null"].ToString();
                    string key = dr["key"].ToString();
                    string extra = dr["extra"].ToString();
                    string comment = dr["comment"].ToString();

                    result += "`" + field + "`";
                    result += " ";
                    result += type + " ";

                    if (nullvalue.ToLower() == "yes")
                    {
                        result += "null" + " ";
                    }
                    else
                    {
                        result += "not null" + " ";
                    }

                    if (key.ToLower() == "pri")
                    {
                        result += " PRIMARY KEY " + " ";
                    }
                    else
                    {
                    }

                    result += "COMMENT '" + extra + "', \n";
                }

                result = result.Substring(0, result.LastIndexOf(","));
                strTrans.Append(result);

                strTrans.Append(" \n");
                strTrans.Append(")  DEFAULT CHARSET=latin1;\n");

                strTrans.Append("\n");
                strTrans.Append("/*Data for the table `student` */\n");

                //*********************************************************
                //cha ru zi duan
                //*********************************************************
                strSql = "select * FROM " + database + "." + table + "";
                dt = MysqlHelper.GetDataTable(strSql);

                string strFields = "";
                string strValues = "";

                foreach (DataColumn dc in dt.Columns)
                {
                    if (dc.AutoIncrement != true)
                    {
                        strFields += "`" + dc.ColumnName + "`,";
                    }
                }

                foreach (DataRow dr in dt.Rows)
                {
                    string strTemp = "";
                    foreach (DataColumn dc in dt.Columns)
                    {
                        if (dc.AutoIncrement != true)
                        {
                            strTemp += "'" + dr[dc.ColumnName].ToString() + "',";
                        }
                    }

                    strValues += "(" + strTemp.Substring(0, strTemp.Length - 1) + ")" + ",";
                }

                strFields = strFields.Substring(0, strFields.Length - 1);
                strValues = strValues.Substring(0, strValues.Length - 1) + ";";
                strTrans.Append("insert into `" + database + "`.`" + table + "` (" + strFields + ") values " + strValues + "\n");

                strTrans.Append("\n");

                return null;
            }

            #endregion MyRegion

            //*********************************************************
            //
            //*********************************************************
            else if (str_type == "sqlserver")
            {
                StringBuilder strTrans = new StringBuilder();

                //string database = treeView1.SelectedNode.Parent.Text;
                //string table = treeView1.SelectedNode.Text;

                //*********************************************************
                //jianku
                //*********************************************************
                strTrans.Append("USE " + database + ";\n");
                strTrans.Append("\n");
                strTrans.Append("/*Table structure for table`" + table + "` */\n");
                strTrans.Append("\n");
                strTrans.Append("DROP TABLE " + table + ";\n");
                strTrans.Append("CREATE TABLE " + table + " (\n");

                string result = "";

                //string strSql = "SHOW full COLUMNS FROM " + database + "." + table + "";
                string strSql = "";
                strSql = "use " + database + ";select   a.[name] as lieming,b.[name] as leixing ,a.prec as length   from   syscolumns a,systypes b  where   a.id=object_id('" + database + ".." + table + "') and a.xtype=b.xtype and    b.[name]<>'sysname'";

                DataTable dt = SqlServerHelper.GetDataTable(strSql);
                foreach (DataRow dr in dt.Rows)
                {
                    if (dr["lieming"].ToString() == "ID")
                    {
                        result += "ID int identity(1,1) primary key,";
                        continue;
                    }

                    string field = dr["lieming"].ToString();
                    string type = dr["leixing"].ToString();
                    string length = dr["length"].ToString();

                    result += "" + field + "";
                    result += " ";
                    if (type.IndexOf("char") != -1)
                    {
                        result += type + "(" + length + ")" + " ,";
                    }
                    else
                    {
                        result += type + " ,";
                    }
                }

                result = result.Substring(0, result.LastIndexOf(","));
                strTrans.Append(result);

                strTrans.Append(" \n");
                strTrans.Append(")  \n");

                strTrans.Append("\n");
                strTrans.Append("/*Data for the table `student` */\n");

                //*********************************************************
                //cha ru zi duan
                //*********************************************************
                if (textBox_config.Text != "")
                {
                    strSql = "select top " + textBox_config.Text + " * FROM " + database + ".." + table + "";
                }
                else
                {
                    strSql = "select * from " + database + ".." + table + "";
                }
                dt = SqlServerHelper.GetDataTable(strSql);

                if (dt.Rows.Count != 0)
                {
                    string strFields = "";
                    string strValues = "";

                    foreach (DataColumn dc in dt.Columns)
                    {
                        if (dc.AutoIncrement != true && dc.ColumnName != "ID")
                        {
                            strFields += "" + dc.ColumnName + ",";
                        }
                    }

                    foreach (DataRow dr in dt.Rows)
                    {
                        string strTemp = "";
                        foreach (DataColumn dc in dt.Columns)
                        {
                            if (dc.AutoIncrement != true && dc.ColumnName != "ID")
                            {
                                if (dc.DataType.FullName == "System.Decimal")
                                {
                                    strTemp += "" + (dr[dc.ColumnName].ToString() == "" ? "null" : dr[dc.ColumnName].ToString()) + ",";
                                }
                                else
                                {
                                    strTemp += "'" + dr[dc.ColumnName].ToString().Replace("'", "''") + "',";
                                }
                            }
                        }

                        strValues += "(" + strTemp.Substring(0, strTemp.Length - 1) + ")" + ",";
                    }

                    strFields = strFields.Substring(0, strFields.Length - 1);
                    strValues = strValues.Substring(0, strValues.Length - 1) + ";";
                    strTrans.Append("insert into " + database + ".." + table + " (" + strFields + ") values " + strValues + "\n");

                    strTrans.Append("\n");
                }

                return strTrans.ToString();
            }

            return null;
        }

        private static void xiewenjian(String strTrans)
        {
            //*********************************************************
            //xie wen jian
            //*********************************************************
            string strDes = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
            strDes = strDes.Replace(@"\\", @"\");
            strDes = Path.Combine(strDes, "1.sql");

            Encoding encode = Encoding.UTF8;
            StreamWriter sw = new StreamWriter(strDes, false, encode);
            sw.Write(strTrans.ToString());
            sw.Flush();
            sw.Close();

            Process.Start(strDes);
        }

        private static void xiewenjian(StringBuilder strTrans)
        {
            //*********************************************************
            //xie wen jian
            //*********************************************************
            xiewenjian(strTrans.ToString());
        }

        private string daochu_table()
        {
            string database = treeView1.SelectedNode.Parent.Text;
            string table = treeView1.SelectedNode.Text;

            return daochu_table(database, table);
        }

        /// <summary>
        /// 导出
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            if (this.treeView1.SelectedNode.Tag.ToString() == "table")
            {
                string strTrans = daochu_table();
                xiewenjian(strTrans);
            }
            else if (this.treeView1.SelectedNode.Tag.ToString() == "database")
            {
                string strSql = "use " + treeView1.SelectedNode.Text + ";" + "select * from sysobjects where type='U'";
                string strTrans = "";
                System.Data.SqlClient.SqlDataReader thisReader = SqlServerHelper.GetReader(strSql);
                while (thisReader.Read())
                {
                    strTrans += daochu_table(this.treeView1.SelectedNode.Text, thisReader[0].ToString());
                }
                xiewenjian(strTrans);
            }
        }

        /// <summary>
        /// 导入
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripLabel3_Click(object sender, EventArgs e)
        {
            string strDes = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
            strDes = strDes.Replace(@"\\", @"\");
            strDes = Path.Combine(strDes, "1.sql");

            Encoding encode = Encoding.UTF8;
            StreamReader sw = new StreamReader(strDes);

            string strResult = sw.ReadToEnd();
            sw.Close();

            ExcuteSql(strResult);

            MessageBox.Show("ok");
        }

        /// <summary>
        ///add
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            //insert into grade(学号，姓名，数学) values(123,"周润发"，70)

            StringBuilder strTrans = new StringBuilder();

            this.richTextBox1.Text = "";
            if (this.treeView1.SelectedNode == null || this.treeView1.SelectedNode.Tag != "table")
            {
                strTrans.Append("insert into grade(学号，姓名，数学) values(123,\"周润发\"，70)\n");
                this.richTextBox1.Text = strTrans.ToString();
                return;
            }
            else
            {
                if (str_type == "mysql")
                {
                    string database = this.treeView1.SelectedNode.Parent.Text;
                    string table = this.treeView1.SelectedNode.Text;

                    //string strSql = "select * FROM " + database + "." + table + "";
                    //DataTable dt = MysqlHelper.GetDataTable(strSql);

                    string strSql = "SHOW full COLUMNS FROM " + database + "." + table + "";
                    DataTable dt = MysqlHelper.GetDataTable(strSql);

                    string strFields = "";
                    string strValues = "";

                    foreach (DataRow dr in dt.Rows)
                    {
                        string field = dr["field"].ToString();
                        string type = dr["type"].ToString();
                        string nullvalue = dr["null"].ToString();
                        string key = dr["key"].ToString();
                        string extra = dr["extra"].ToString();
                        string comment = dr["comment"].ToString();

                        if (extra.IndexOf("auto_increment") != -1)
                        {
                            continue;
                        }

                        strFields += "`" + field + "`,";
                        strValues += "'1',";
                    }

                    if (strValues.Trim() != "")
                    {
                        strFields = strFields.Substring(0, strFields.Length - 1);
                        strValues = strValues.Substring(0, strValues.Length - 1);

                        strTrans.Append("insert into `" + database + "`.`" + table + "`(" + strFields + ") values(" + strValues + ")\n");
                    }

                    this.richTextBox1.Text = strTrans.ToString();
                    return;
                }
                else
                {
                    strTrans.Append("insert into grade(学号，姓名，数学) values(123,\"周润发\"，70)\n");
                    this.richTextBox1.Text = strTrans.ToString();
                    return;
                }
            }
        }

        /// <summary>
        /// delete
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripButton3_Click(object sender, EventArgs e)
        {
            //delete from grade where 数学＝0

            StringBuilder strTrans = new StringBuilder();

            this.richTextBox1.Text = "";

            if (this.treeView1.SelectedNode == null || this.treeView1.SelectedNode.Tag != "table")
            {
                strTrans.Append("delete from grade where 数学＝0");
                this.richTextBox1.Text = strTrans.ToString();
                return;
            }
            else
            {
                string database = this.treeView1.SelectedNode.Parent.Text;
                string table = this.treeView1.SelectedNode.Text;

                if (str_type == "sqlserver")
                {
                    strTrans.Append("delete from " + database + ".." + table + "");
                }
                else
                {
                    strTrans.Append("delete from `" + database + "`.`" + table + "`");
                }
                this.richTextBox1.Text = strTrans.ToString();
                return;
            }
        }

        /// <summary>
        /// update
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripButton4_Click(object sender, EventArgs e)
        {
            //update grade set 数学＝数学+10

            StringBuilder strTrans = new StringBuilder();

            this.richTextBox1.Text = "";
            if (this.treeView1.SelectedNode == null || this.treeView1.SelectedNode.Tag != "table")
            {
                strTrans.Append("update grade set 数学＝数学+10 where id=1\n");
                this.richTextBox1.Text = strTrans.ToString();
                return;
            }
            else
            {
                string database = this.treeView1.SelectedNode.Parent.Text;
                string table = this.treeView1.SelectedNode.Text;

                //string strSql = "select * FROM " + database + "." + table + "";
                //DataTable dt = MysqlHelper.GetDataTable(strSql);

                string strSql = "SHOW full COLUMNS FROM " + database + "." + table + "";
                DataTable dt = MysqlHelper.GetDataTable(strSql);

                string strFields = "";
                string strValues = "";

                foreach (DataRow dr in dt.Rows)
                {
                    string field = dr["field"].ToString();
                    string type = dr["type"].ToString();
                    string nullvalue = dr["null"].ToString();
                    string key = dr["key"].ToString();
                    string extra = dr["extra"].ToString();
                    string comment = dr["comment"].ToString();

                    if (extra.IndexOf("auto_increment") != -1)
                    {
                        continue;
                    }

                    strFields += "`" + field + "`='1',";
                }

                strFields = strFields.Substring(0, strFields.Length - 1);
                //strValues = strValues.Substring(0, strValues.Length - 1);

                strTrans.Append("update  `" + database + "`.`" + table + "`set " + strFields + "");
                this.richTextBox1.Text = strTrans.ToString();
                return;
            }
        }

        /// <summary>
        /// select
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripButton5_Click(object sender, EventArgs e)
        {
            //update grade set 数学＝数学+10

            StringBuilder strTrans = new StringBuilder();

            this.richTextBox1.Text = "";
            if (this.treeView1.SelectedNode == null || this.treeView1.SelectedNode.Tag != "table")
            {
                strTrans.Append("select * from sys.sysobjects   where id=1\n");
                this.richTextBox1.Text = strTrans.ToString();
                return;
            }
            else
            {
                if (str_type == "mysql")
                {
                    string database = this.treeView1.SelectedNode.Parent.Text;
                    string table = this.treeView1.SelectedNode.Text;

                    string strSql = "SHOW full COLUMNS FROM " + database + "." + table + "";
                    DataTable dt = MysqlHelper.GetDataTable(strSql);

                    string strFields = "";
                    string strValues = "";

                    foreach (DataRow dr in dt.Rows)
                    {
                        string field = dr["field"].ToString();
                        string type = dr["type"].ToString();
                        string nullvalue = dr["null"].ToString();
                        string key = dr["key"].ToString();
                        string extra = dr["extra"].ToString();
                        string comment = dr["comment"].ToString();

                        //if (extra.IndexOf("auto_increment") != -1)
                        //{
                        //    continue;
                        //}

                        strFields += "`" + field + "`,";
                    }

                    strFields = strFields.Substring(0, strFields.Length - 1);
                    //strValues = strValues.Substring(0, strValues.Length - 1);

                    strTrans.Append("select " + strFields + " from `" + database + "`.`" + table + "`");
                    this.richTextBox1.Text = strTrans.ToString();
                    return;
                }
                else if (str_type == "sqlserver")
                {
                    string strDataBase = this.treeView1.SelectedNode.Parent.Text;
                    string strTable = this.treeView1.SelectedNode.Text;

                    //string strSql = "SHOW full COLUMNS FROM " + database + "." + table + "";
                    //DataTable dt = MysqlHelper.GetDataTable(strSql);
                    string strSql = "use " + strDataBase + ";select   a.[name] as lieming,b.[name] as leixing    from   syscolumns a,systypes b  where   a.id=object_id('" + strDataBase + ".." + strTable + "') and a.xtype=b.xtype and    b.[name]<>'sysname'";
                    DataTable dt = SqlServerHelper.GetDataTable(strSql);

                    string strFields = "";
                    string strValues = "";

                    foreach (DataRow dr in dt.Rows)
                    {
                        string field = dr["lieming"].ToString();
                        string type = dr["leixing"].ToString();

                        strFields += "" + field + ",";
                    }

                    strFields = strFields.Substring(0, strFields.Length - 1);
                    //strValues = strValues.Substring(0, strValues.Length - 1);

                    string top = this.textBox_config.Text == "" ? "" : "top " + this.textBox_config.Text + " ";
                    strTrans.Append("select " + top + strFields + " from " + strDataBase + ".." + strTable + "");
                    this.richTextBox1.Text = strTrans.ToString();
                    return;
                }
            }
        }

        /// <summary>
        /// alter
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripButton6_Click(object sender, EventArgs e)
        {
            //alter table table1 add No_id varchar(50)  null
            StringBuilder strTrans = new StringBuilder();

            this.richTextBox1.Text = "";

            if (this.treeView1.SelectedNode == null || this.treeView1.SelectedNode.Tag != "table")
            {
                strTrans.Append("/*\n");
                strTrans.Append("mysql> CREATE TABLE t1 (a INTEGER,b CHAR(10));\n");
                strTrans.Append("把表t1重新命名为t2：\n");
                strTrans.Append("\n");
                strTrans.Append("mysql> ALTER TABLE t1 RENAME t2;\n");
                strTrans.Append("把列a从INTERGER更改为TINYINT NOT NULL（名称保持不变），并把列b从CHAR(10)更改为CHAR(20)，同时把列b重新命名为列c：\n");
                strTrans.Append("\n");
                strTrans.Append("mysql> ALTER TABLE t2 MODIFY a TINYINT NOT NULL, CHANGE b c CHAR(20);\n");
                strTrans.Append("添加一个新的TIMESTAMP列，名称为d：\n");
                strTrans.Append("\n");
                strTrans.Append("mysql> ALTER TABLE t2 ADD d TIMESTAMP;\n");
                strTrans.Append("在列d和列a中添加索引：\n");
                strTrans.Append("\n");
                strTrans.Append("mysql> ALTER TABLE t2 ADD INDEX (d), ADD INDEX (a);\n");
                strTrans.Append("删除列c：\n");
                strTrans.Append("\n");
                strTrans.Append("mysql> ALTER TABLE t2 DROP COLUMN c;\n");
                strTrans.Append("添加一个新的AUTO_INCREMENT整数列，名称为c：\n");
                strTrans.Append("\n");
                strTrans.Append("mysql> ALTER TABLE t2 ADD c INT UNSIGNED NOT NULL AUTO_INCREMENT,\n");
                strTrans.Append("    ->     ADD PRIMARY KEY (c);\n");
                strTrans.Append("*/\n");

                this.richTextBox1.Text = strTrans.ToString();
                return;
            }
            else
            {
                string database = this.treeView1.SelectedNode.Parent.Text;
                string table = this.treeView1.SelectedNode.Text;

                strTrans.Append("alter  table `" + database + "`.`" + table + "` add No_id varchar(50)  null ");

                strTrans.Append("\n");

                strTrans.Append("/*\n");
                strTrans.Append("mysql> CREATE TABLE t1 (a INTEGER,b CHAR(10));\n");
                strTrans.Append("把表t1重新命名为t2：\n");
                strTrans.Append("\n");
                strTrans.Append("mysql> ALTER TABLE t1 RENAME t2;\n");
                strTrans.Append("把列a从INTERGER更改为TINYINT NOT NULL（名称保持不变），并把列b从CHAR(10)更改为CHAR(20)，同时把列b重新命名为列c：\n");
                strTrans.Append("\n");
                strTrans.Append("mysql> ALTER TABLE t2 MODIFY a TINYINT NOT NULL, CHANGE b c CHAR(20);\n");
                strTrans.Append("添加一个新的TIMESTAMP列，名称为d：\n");
                strTrans.Append("\n");
                strTrans.Append("mysql> ALTER TABLE t2 ADD d TIMESTAMP;\n");
                strTrans.Append("在列d和列a中添加索引：\n");
                strTrans.Append("\n");
                strTrans.Append("mysql> ALTER TABLE t2 ADD INDEX (d), ADD INDEX (a);\n");
                strTrans.Append("删除列c：\n");
                strTrans.Append("\n");
                strTrans.Append("mysql> ALTER TABLE t2 DROP COLUMN c;\n");
                strTrans.Append("添加一个新的AUTO_INCREMENT整数列，名称为c：\n");
                strTrans.Append("\n");
                strTrans.Append("mysql> ALTER TABLE t2 ADD c INT UNSIGNED NOT NULL AUTO_INCREMENT,\n");
                strTrans.Append("    ->     ADD PRIMARY KEY (c);\n");
                strTrans.Append("*/\n");

                this.richTextBox1.Text = strTrans.ToString();
                return;
            }
        }

        /// <summary>
        /// drop
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripButton7_Click(object sender, EventArgs e)
        {
            //DROP TABLE  `name` ;

            StringBuilder strTrans = new StringBuilder();

            this.richTextBox1.Text = "";

            if (this.treeView1.SelectedNode == null || this.treeView1.SelectedNode.Tag != "table")
            {
                strTrans.Append("DROP TABLE  `name` ; ");
                this.richTextBox1.Text = strTrans.ToString();
                return;
            }
            else
            {
                string database = this.treeView1.SelectedNode.Parent.Text;
                string table = this.treeView1.SelectedNode.Text;

                strTrans.Append("DROP TABLE   `" + database + "`.`" + table + "`; ");

                strTrans.Append("\n");

                this.richTextBox1.Text = strTrans.ToString();
                return;
            }
        }

        private void 综合_MouseClick(object sender, MouseEventArgs e)
        {
        }

        private void treeView1_Click(object sender, EventArgs e)
        {
        }

        private void treeView1_MouseClick(object sender, MouseEventArgs e)
        {
            try
            {
                if (e.Button.ToString() == "Right")
                {
                    string strTemp = this.treeView1.SelectedNode.Text;

                    string strTextBox = richTextBox1.Text;
                    string strResult = "";

                    int i = this.richTextBox1.SelectionStart;
                    strResult = strTextBox.Substring(0, i);
                    strResult += strTemp;
                    strResult += strTextBox.Substring(i, strTextBox.Length - i);

                    richTextBox1.Text = strResult;
                    richTextBox1.SelectionStart = i;
                }
            }
            catch (Exception ee) { }
        }

        /// <summary>
        /// create
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripButton8_Click(object sender, EventArgs e)
        {
            StringBuilder strTrans = new StringBuilder();

            strTrans.Append("USE  `test`;\n");
            strTrans.Append("\n");
            strTrans.Append("/*Table structure for table`member` */\n");
            strTrans.Append("\n");
            strTrans.Append("DROP TABLE IF EXISTS`member`;\n");
            strTrans.Append("CREATE TABLE`member` (\n");
            strTrans.Append("`id` int(11) not null  PRIMARY KEY  AUTO_INCREMENT COMMENT '', \n");
            strTrans.Append("`name` varchar(50) null COMMENT '', \n");
            strTrans.Append("`pass` varchar(50) null COMMENT '', \n");
            strTrans.Append("`rank` int(11) null COMMENT '', \n");
            strTrans.Append("`status` int(1) null COMMENT '' \n");
            strTrans.Append(")  DEFAULT CHARSET=latin1;\n");
            strTrans.Append("\n");
            strTrans.Append("\n");
            strTrans.Append("/*\n");
            strTrans.Append("int(10)\n");
            strTrans.Append("text\n");
            strTrans.Append("varchar(30)\n");
            strTrans.Append("datetime\n");
            strTrans.Append("*/\n");

            this.richTextBox1.Text = strTrans.ToString();
            return;
        }

        private void charu_tabledetail()
        {
            StringBuilder strTrans = new StringBuilder();
            this.richTextBox1.Text = "";

            if (this.treeView1.SelectedNode == null || this.treeView1.SelectedNode.Tag != "table")
            {
                strTrans.Append("insert into table_detail(table_name,`field`,name) values(123,'周润发',70)\n");
                this.richTextBox1.Text = strTrans.ToString();
                return;
            }
            else
            {
                string database = this.treeView1.SelectedNode.Parent.Text;
                string table = this.treeView1.SelectedNode.Text;

                strTrans.Append("use " + database + ";\n");

                string strSql = "SHOW full COLUMNS FROM " + database + "." + table + "";
                DataTable dt = MysqlHelper.GetDataTable(strSql);

                string strFields = "";
                string strValues = "";

                foreach (DataRow dr in dt.Rows)
                {
                    string field = dr["field"].ToString();
                    string type = dr["type"].ToString();
                    string nullvalue = dr["null"].ToString();
                    string key = dr["key"].ToString();
                    string extra = dr["extra"].ToString();
                    string comment = dr["comment"].ToString();

                    strValues += "('" + table + "','" + field + "','" + comment + "')" + ",";
                }

                //strFields = strFields.Substring(0, strFields.Length - 1);
                strValues = strValues.Substring(0, strValues.Length - 1);

                strTrans.Append("insert into table_detail(table_name,`field`,name) values" + strValues + "\n");
                //strTrans.Append("update  `" + database + "`.`" + table + "`set " + strFields + "");
                this.richTextBox1.Text = strTrans.ToString();
                return;
            }
        }

        private void panel1_Paint(object sender, PaintEventArgs e)
        {
        }

        /// <summary>
        /// 导出选中行
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripButton10_Click(object sender, EventArgs e)
        {
            this.richTextBox1.Text = "";

            DataTable dt = this.dataGridView1.DataSource as DataTable;
            foreach (DataGridViewRow item in dataGridView1.Rows)
            {
                string strFields = "(";
                string strValues = "(";

                if (item.Selected == true)
                {
                    //item.Cells[0].Value
                    for (int i = 0; i < item.Cells.Count; i++)
                    {
                        strFields += dataGridView1.Columns[i].Name + ",";
                        if (item.Cells[i].ValueType.FullName == "System.Decimal")
                        {
                            strValues += item.Cells[i].Value.ToString() == "" ? "null" + "," : item.Cells[i].Value + ",";
                        }
                        else
                        {
                            strValues += "'" + item.Cells[i].Value + "',";
                        }

                        // dc.ToString()
                    }
                    strFields = strFields.Substring(0, strFields.Length - 1);
                    strFields += ")";
                    strValues = strValues.Substring(0, strValues.Length - 1);
                    strValues += ")";

                    string temp = "insert into table " + strFields + " values " + strValues + ";";
                    this.richTextBox1.Text += temp + "\n";
                }
            }
        }

        private void buttonBean_Click(object sender, EventArgs e)
        {
        }

        private void button2_Click(object sender, EventArgs e)
        {
        }

        private void 插入tabledetailToolStripMenuItem_Click(object sender, EventArgs e)
        {
            charu_tabledetail();
        }

        private void returnItemDTOToolStripMenuItem_Click(object sender, EventArgs e)
        {
        }

        private void returnItemDTOToolStripMenuItemToolStripMenuItem_Click(object sender, EventArgs e)
        {
            zuzhi_returnItemDTO();
        }

        private void zuzhi_returnItemDTO()
        {
            //1 判断table名 2得到选中列 3按照定义字段顺序 得到选中列中值组织代码
            Dictionary<string, string> d = new Dictionary<string, string>();
            d.Add("ProductId", "");
            d.Add("SKUId", "ProductCode");
            d.Add("ProductName", "ProductName");
            d.Add("Unit", "Unit");

            d.Add("Amount", "OrderAmount");
            d.Add("OrigPrice", "");
            d.Add("Price", "SellPrice");
            d.Add("Discount", "Discount");

            d.Add("PointsSpent", "Points");
            d.Add("PointsEarned", "PresentPoint");
            d.Add("Size", "Size");
            d.Add("ReasonId", "");

            d.Add("Id", "");
        }

        private void zuzhi_returnItemDTO_Ex()
        {
            //1 判断table名 2得到选中列 3按照定义字段顺序 得到选中列中值组织代码
            Dictionary<string, string> d = new Dictionary<string, string>();
            d.Add("ProductId", "");
            d.Add("SKUId", "ProductCode");
            d.Add("ProductName", "ProductName");
            d.Add("Unit", "Unit");

            d.Add("Amount", "OrderAmount");
            d.Add("OrigPrice", "");
            d.Add("Price", "SellPrice");
            d.Add("Discount", "Discount");

            d.Add("PointsSpent", "Points");
            d.Add("PointsEarned", "PresentPoint");
            d.Add("Size", "Size");
            d.Add("ReasonId", "");

            d.Add("Id", "");

            StringBuilder sb = new StringBuilder();
            sb.AppendLine("ReturnItemDTO_Ex returnItemDTO_Ex = new ReturnItemDTO_Ex()");

            zuzhi_ziduan(d, sb);
        }

        private void afterSalesOrderItemDTOToolStripMenuItem_Click(object sender, EventArgs e)
        {
            zuzhi_afterSalesOrderItemDTO();
        }

        private static void zuzhi_afterSalesOrderItemDTO()
        {
            Dictionary<string, string> d = new Dictionary<string, string>();
            d.Add("IsSpecial", "IsSpecial");
            d.Add("SuitSerialNumber", "");
            d.Add("SuitIdInSwapOrder", "");
            d.Add("IsUpgrade", "");

            d.Add("SpecialOfferType", "");
            d.Add("WareHouseType", "");
        }

        private void DeliveryAftersaleDTOToolStripMenuItem_Click(object sender, EventArgs e)
        {
            zuzhi_DeliveryAftersaleDTO();
        }

        private static void zuzhi_DeliveryAftersaleDTO()
        {
            Dictionary<string, string> d = new Dictionary<string, string>();
            d.Add("Province", "Province");
            d.Add("City", "City");
            d.Add("Area", "Area");
            d.Add("Addressee", "Addressee");
        }

        private void AcceptApplicationDTOtoolStripMenuItem_Click(object sender, EventArgs e)
        {
            zuzhi_AcceptApplicationDTO();
        }

        private void zuzhi_AcceptApplicationDTO()
        {
            Dictionary<string, string> d = new Dictionary<string, string>();
            d.Add("UserId", "UserID");
            d.Add("PaymentTypeID", "PaymentTypeID");
            d.Add("PaymentType", "PaymentType");

            StringBuilder sb = new StringBuilder();
            sb.AppendLine("AcceptApplicationDTO acceptApplicationDto = new AcceptApplicationDTO()");
            sb.AppendLine("{");

            foreach (DataGridViewRow item in dataGridView1.Rows)
            {
                if (item.Selected == true)
                {
                    for (int i = 0; i < item.Cells.Count; i++)
                    {
                        foreach (KeyValuePair<string, string> ie in d)
                        {
                            if (ie.Value == dataGridView1.Columns[i].Name)
                            {
                                if (item.Cells[i].ValueType.FullName == "System.Decimal" || item.Cells[i].ValueType.FullName.IndexOf("System.Int") != -1)
                                {
                                    sb.AppendLine(ie.Key + "=" + item.Cells[i].Value.ToString());
                                }
                                else
                                {
                                    sb.AppendLine(ie.Key + "=\"" + item.Cells[i].Value.ToString() + "\"");
                                }
                            }
                        }
                    }
                }
            }

            sb.AppendLine("}");

            this.richTextBox1.Text += sb.ToString();
        }

        private void zuzhi_AcceptApplicationDTO_Ex()
        {
            Dictionary<string, string> d = new Dictionary<string, string>();
            d.Add("UserId", "UserID");
            //d.Add("PaymentTypeID", "PaymentTypeID");
            //d.Add("PaymentType", "PaymentType");

            StringBuilder sb = new StringBuilder();
            sb.AppendLine("AcceptApplicationDTO_Ex acceptApplicationDto = new AcceptApplicationDTO_Ex()");

            zuzhi_ziduan(d, sb);
        }

        private void zuzhi_ziduan(Dictionary<string, string> d, StringBuilder sb)
        {
            sb.AppendLine("{");
            foreach (DataGridViewRow item in dataGridView1.Rows)
            {
                if (item.Selected == true)
                {
                    for (int i = 0; i < item.Cells.Count; i++)
                    {
                        foreach (KeyValuePair<string, string> ie in d)
                        {
                            if (ie.Value == dataGridView1.Columns[i].Name)
                            {
                                if (item.Cells[i].ValueType.FullName == "System.Decimal" || item.Cells[i].ValueType.FullName.IndexOf("System.Int") != -1)
                                {
                                    sb.AppendLine(ie.Key + "=" + item.Cells[i].Value.ToString() + ",");
                                }
                                else
                                {
                                    sb.AppendLine(ie.Key + "=\"" + item.Cells[i].Value.ToString() + "\"" + ",");
                                }
                            }
                        }
                    }
                }
            }
            string strreturn = sb.ToString();
            if (strreturn.LastIndexOf(',') != -1)
            {
                strreturn = strreturn.Substring(0, strreturn.LastIndexOf(','));
            }
            strreturn += "\n};\n";
            //sb.AppendLine("};");

            this.richTextBox1.Text += strreturn;
        }

        private void acceptApplicationDTOExToolStripMenuItem_Click(object sender, EventArgs e)
        {
            zuzhi_AcceptApplicationDTO_Ex();
        }

        private void returnItemDTOExToolStripMenuItem_Click(object sender, EventArgs e)
        {
            zuzhi_returnItemDTO_Ex();
        }

        /// <summary>
        /// 打开
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog1 = new OpenFileDialog();
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                this.textBox_file.Text = openFileDialog1.FileName;
            }
        }

        private void deliveryAftersaleDTOExToolStripMenuItem_Click(object sender, EventArgs e)
        {
        }

        private void daochuToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DataTable dt = common.SqliteHelper.GetDataTable("select * from search");
            this.dataGridViewTable.DataSource = dt;

            //生成文件 每一项是一行
        }
    }
}